/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/13 00:59
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

(function () {

    "use strict";

    var api = {
        "getGroupLists": '/stream/group/getGroupLists',
        "doAddDevice": '/stream/device/doAddDevice',
        "deviceLists": "/stream/device/getLists"
    };


    sm.controller('admin-stream-device-controller', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {


        // 添加设备信息
        $scope.add = {
            "groupId": 1,
            "groupName": "默认",
            "deviceName": "",
            "deviceSerial": "",
            "validateCode": ""
        };
        $scope.groupLists = [];
        $scope.deviceLists = [];
        $scope.addDeviceError = '';
        $scope.showMoreDevice = false;
        $scope.device = {
            page: 1,
            keyword: ""
        };
        $scope.groupWithId = {};

        /**
         * 分组
         */
        // 保存在分组信息获取到之后执行
        function groupLists() {
            return $http.get(api.getGroupLists);
        }

        groupLists().then(function (res) {
            if (res.data.state) {
                $scope.groupLists = res.data.data;
                angular.forEach(res.data.data, function (val) {
                    var id = val['id'];
                    $scope.groupWithId[id] = val['groupName'];
                });
            }
            // 获取列表
            getDeviceLists();
        });

        /**
         * 选择分组
         * @param $index
         */
        $scope.selectGroup = function ($index) {
            $scope.add.groupId = $scope.groupLists[$index]['id'];
            $scope.add.groupName = $scope.groupLists[$index]['groupName'];
        };

        $scope.editDevice = function () {
            $('#mangeDevice').modal();
        };

        /**
         * 添加直播
         */
        $scope.addDevice = function () {
            $scope.addDeviceError = '';
            $("#addDevice").modal();
        };

        $scope.doAddDevice = function () {

            var dev = $scope.add;
            if (!dev.groupId || !dev.groupName || !dev.deviceName || !dev.deviceSerial || !dev.validateCode) {
                $scope.addDeviceError = '有必填字段没有填写';
                return false;
            }

            if (!/^\d{9}$/.test(dev.deviceSerial)) {
                $scope.addDeviceError = '序列号格式错误';
                return false;
            }

            if (!/^\w{6}$/.test(dev.validateCode)) {
                $scope.addDeviceError = '验证码格式错误';
                return false;
            }

            $http.post(api.doAddDevice, {
                "groupId": $scope.add.groupId,
                "deviceName": $scope.add.deviceName,
                "deviceSerial": $scope.add.deviceSerial,
                "validateCode": $scope.add.validateCode
            }).success(function (res) {
                if (res.state) {
                    $scope.adddeviceer = '';
                } else {
                    $scope.adddeviceer = res.error ? res.error : '设备添加失败';
                }
            });
        };

        function getDeviceLists() {
            $http.get(api.deviceLists, {
                params: $scope.device
            }).success(function (res) {
                if (res.state) {
                    $scope.device.showMoreDevice = res.data.nextPage;
                    appendLists($scope.deviceLists, res.data.data);
                } else {
                    $scope.device.showMoreDevice = false;
                }
            });
        }

        function appendLists(lists, appendList) {
            if (appendList.length == 0) {
                return false;
            }

            angular.forEach(appendList, function (v) {
                lists.push(v);
            });
        }


    }]);


})();